package com.qfjy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.qfjy.client.VideoInfoFeignClient;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.service.MeetingPubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Slf4j
@RequestMapping("meetingPub")
public class MeetingPubController {

    @Autowired
    private MeetingPubService meetingPubService;


    /**
     * TODO 根据会议编号查询会议信息  meetingPub/select?pcode=1111
     * @return
     */
    @ResponseBody
    @GetMapping("select")
   // @SentinelResource(value = "/meetingPub/select/sentinel")
    public Object selectMeetingPubByPcode(@RequestParam("pcode")String pcode){

       MeetingPub meetingPub= meetingPubService.selectMeetingPubByPcode(pcode);
       log.info("查询的是会议微服务信息：{}",meetingPub);

       //要给的是一个ResultJson 统一内容格式（统一内容协议）

        //根据会议编号 得到视频召开信息   RestTemplate方式完成服务通信
        //String url="http://localhost:8085/videoInfo/select?pcode="+pcode;

        /**
         * Ribbon+RestTemplate服务间通信
         * Spring Cloud中，当Ribbon与Nacos配合使用时，
         * Ribbon可自动从Nacos Server获取服务提供者地址列表，并基于负载均衡算法，请求其中一个服务提供者实例。
         */
       // String url="http://qf-video/videoInfo/select?pcode="+pcode;
      //  String result= restTemplate.getForObject(url,String.class);

        /**
         * Feign方式服务间通信： 使用Feign调用API就像调用本地方法一样
         * Sentinel集成Feign实现服务间通信（
         */
        String result=videoInfoFeignClient.selectVideoInfoByPcode(pcode);
        log.info("会议微服务 内部服务间通信调用视频微服务--》{}",result);

        meetingPub.setRemark(result);

        String result1=  videoInfoFeignClient.selectVideoInfoByPcode1(pcode);
            log.info("视频微服务调用--》pcode1方法--》{}",result1);
        return meetingPub;
    }
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private VideoInfoFeignClient videoInfoFeignClient;


    @GetMapping("info")
    @ResponseBody
    public String info(){
        log.info("请求后台info方法-->");

        return "Hello info";
    }

    @GetMapping("infoThread")
    @ResponseBody
    public String infoThread() throws InterruptedException {
        log.info("请求后台infoThread方法-->");


        return "Hello infoThread 并发线程数测试---》正常返回";
    }

    @GetMapping("info1")
    @ResponseBody
    public String info1(){
        log.info("info1 --->");
        return "Hello info1";
    }
    @GetMapping("info2")
    @ResponseBody
    public String info2(){
        log.info("info2 --->");
        return "Hello info2";
    }


    @GetMapping("info3")
    @ResponseBody
    public String info3(@RequestParam(value = "flag",required = false) String flag) throws Exception {
       //业务逻辑
        return "Hello info3 慢调用比例";
    }


    @GetMapping("info5")
    @ResponseBody
    @SentinelResource(value = "/meetingPub/info5/sentinel")
    public String info5(@RequestParam(value = "flag",required = false) String flag) throws  Exception  {
        log.info("info5 --->热点参数 走这个方法了--》");
        return "Hello info5 热点参数  正确返回==>"+ flag;
    }

    @GetMapping("info6")
    @ResponseBody
    @SentinelResource(value = "/meetingPub/info6/sentinel")
    public String info6(){
        log.info("info6 --->");
        return "Hello info6";
    }

    /**
     * #TODO 默认info6 自定 触发规则自定义方法
     *    Blocked by Sentinel (flow limiting)
     *    返回类型需要与原方法相匹配，参数类型需要和原方法相匹配并且最后加一个额外的参数，类型为 `BlockException`
     */

    //熔断降级 （异常数）
    @GetMapping("info7")
    @ResponseBody
    @SentinelResource(value = "/meetingPub/info7/sentinel")
    public String info7(@RequestParam(value = "flag",required = false) String flag)   {

        log.info("info7方法 执行了---》");
        if(flag==null){
            throw new RuntimeException("传的参数为null");
        }
        return "Hello info7  正确请求并响应返回"+flag;
    }


}
